package com.dhcc.sds.base.controller;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.entity.SdsQcentityRuleEntity;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsExpressService;
import com.dhcc.sds.base.service.ISdsQcentityRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;


/**
 * 病种筛查配置
 * 
 * @ClassName: SdsQcentityRuleController
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:49
 */
@Controller
@RequestMapping("${sys.backendPath}/sds/sdsqcentityrule")
public class SdsQcentityRuleController extends BaseController {

    private String PREFIX = "/backend/sds/sdsqcentityrule/";

    @Autowired
    private ISdsQcentityRuleService sdsQcentityRuleService;

    @Autowired
    private ISdsExpressService sdsExpressService;

    @Autowired
    private ISdsCommonService sdsCommonService;

    /**
     * 跳转到病种筛查配置列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        //获取科室信息
        JSONObject deptMap = sdsCommonService.findDeptList();
        //获取入组筛查函数列表
        Map<String, Object> inCheckFuncs = sdsExpressService.findExpsByCode("QCRule");
        model.addAttribute("deptMap", deptMap);
        model.addAttribute("expMap", inCheckFuncs);
        return PREFIX + "sdsqcentityrule.html";
    }

    /**
     * 跳转到新增病种筛查配置
     */
    @RequestMapping("/to_add")
    public String toAdd() {
        return PREFIX + "sdsqcentityrule_add.html";
    }

    /**
     * 跳转到修改病种筛查配置
     */
    @RequestMapping("/to_update/{sdsQcentityRuleId}")
    public String toUpdate(@PathVariable Long sdsQcentityRuleId, Model model) {
        SdsQcentityRuleEntity sdsQcentityRule = this.sdsQcentityRuleService.selectById(sdsQcentityRuleId);
        model.addAttribute("sdsQcentityRule", sdsQcentityRule);
        return PREFIX + "sdsqcentityrule_edit.html";
    }

    /**
     * 获取病种筛查配置列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<SdsQcentityRuleEntity> page = new PageFactory<SdsQcentityRuleEntity>().defaultPage();
        List<SdsQcentityRuleEntity> result = this.sdsQcentityRuleService.page(page, params);
        page.setRecords((List<SdsQcentityRuleEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增病种筛查配置
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SdsQcentityRuleEntity sdsQcentityRule) {
        if (CommonUtil.isOneEmpty(sdsQcentityRule)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        sdsQcentityRule.insert();
        return R.ok();
    }

    /**
     * 删除病种筛查配置
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long sdsQcentityRuleId) {

        this.sdsQcentityRuleService.deleteById(sdsQcentityRuleId);

        return R.ok();
    }

    /**
     * 修改病种筛查配置
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SdsQcentityRuleEntity sdsQcentityRule) {
        if (CommonUtil.isOneEmpty(sdsQcentityRule)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SdsQcentityRuleEntity old = this.sdsQcentityRuleService.selectById(sdsQcentityRule.getId());

        old.setEntityId(sdsQcentityRule.getEntityId());
        old.setRuleType(sdsQcentityRule.getRuleType());
        old.setRuleDesc(sdsQcentityRule.getRuleDesc());
        old.setExpressId(sdsQcentityRule.getExpressId());
        old.setParam(sdsQcentityRule.getParam());
        old.setParam2(sdsQcentityRule.getParam2());
        old.setIndNo(sdsQcentityRule.getIndNo());
        old.setIsActive(sdsQcentityRule.getIsActive());
        old.setLinkDept(sdsQcentityRule.getLinkDept());
        old.setIsOnly(sdsQcentityRule.getIsOnly());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 修改病种筛查配置
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(SdsQcentityRuleEntity sdsQcentityRule) {
        if (CommonUtil.isOneEmpty(sdsQcentityRule)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        try {
            // 这三个因为前台需要输入特殊字符，前台已转码，因此后台也需要转码
            sdsQcentityRule.setRuleDesc(java.net.URLDecoder.decode(sdsQcentityRule.getRuleDesc(), "UTF-8"));
            sdsQcentityRule.setParam(java.net.URLDecoder.decode(sdsQcentityRule.getParam(), "UTF-8"));
            sdsQcentityRule.setParam2(java.net.URLDecoder.decode(sdsQcentityRule.getParam2(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        SdsQcentityRuleEntity old = this.sdsQcentityRuleService.selectById(sdsQcentityRule.getId());
        if (CommonUtil.isEmpty(old)) {
            sdsQcentityRule.insert();
        } else {
            old.setEntityId(sdsQcentityRule.getEntityId());
            old.setRuleType(sdsQcentityRule.getRuleType());
            old.setRuleDesc(sdsQcentityRule.getRuleDesc());
            old.setExpressId(sdsQcentityRule.getExpressId());
            old.setParam(sdsQcentityRule.getParam());
            old.setParam2(sdsQcentityRule.getParam2());
            old.setIndNo(sdsQcentityRule.getIndNo());
            old.setIsActive(sdsQcentityRule.getIsActive());
            old.setLinkDept(sdsQcentityRule.getLinkDept());
            old.setIsOnly(sdsQcentityRule.getIsOnly());
            old.updateAllColumnById();
        }
        return R.ok();
    }

    /**
     * 跳转到导入病种的页面
     */
    @RequestMapping("/to_import")
    public String toImport() {
        return PREFIX + "import.html";
    }

    /**
     * 导入病种字典
     */
    @RequestMapping(method = RequestMethod.POST, path = "/import")
    @ResponseBody
    public R importItem(@RequestPart("file") MultipartFile file) {
        ExcelReader reader;
        try {
            reader = ExcelUtil.getReader(file.getInputStream(),0);
            List<List<Object>> itemAll = reader.read();
            this.sdsQcentityRuleService.importRule(itemAll);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
        return R.ok();
    }
}
